const express = require("express");
const router = express.Router();
const {
  activityCartListModel,
  cartInfoListModel,
  activityRuleModel,
  couponInfoListModel,
} = require("../model/cart");
const { cartListModel, skuProductListModel } = require("../model/product");
// 普通购物车信息
router.get("/cartList", async (req, res) => {
  const data = await cartListModel.find().populate("skuId");
  res.send({
    code: 200,
    msg: "获取成功",
    data,
  });
});

/*
 * 添加、修改商品到购物车，skuNum值+1为递增，-1则为递减.
 *   skuId   - 商品skuId.
 *   skuNum  - 商品数量.
 */
router.get("/addToCart/:skuId/:skuNum", async (req, res) => {
  let { skuId, skuNum } = req.params;
  if (skuNum === "+1") {
    skuNum = 1;
  } else {
    skuNum = -1;
  }
  let data = await cartListModel.findOne({ skuId });
  if (data) {
    data.skuNum = data.skuNum + skuNum;
    if (data.skuNum === 0) {
      // 填充为0时，删除该商品
      await cartListModel.deleteOne({ skuId });
      let cartList = await cartListModel.find().populate("skuId");
      res.send({
        code: 200,
        msg: "添加成功",
        cartList,
      });
      return;
    }
    await data.save();
  } else {
    await cartListModel.create(req.params);
  }
  let cartList = await cartListModel.find().populate("skuId");
  res.send({
    code: 200,
    msg: "添加成功",
    cartList,
  });
});

router.get("/shopaddToCart/:skuId/:skuNum", async (req, res) => {
  let { skuId, skuNum } = req.params;
  let data = await cartListModel.find();
  let skuIdx = data.findIndex((item) => item._id == skuId);
  if (skuIdx != -1) {
    let num = data[skuIdx].skuNum;
    if (skuNum == -1) {
      let newnum = num - 1
      if (newnum == 0) {
        await cartListModel.deleteOne({ _id: skuId })

      } else {
        await cartListModel.updateOne({ _id: skuId }, { skuNum: newnum })

      }
    } else {
      let newnum = num + 1

      await cartListModel.updateOne({ _id: skuId }, { skuNum: newnum })

    }
  } else {
    await cartListModel.create(req.params);
  }
  res.send({
    code: 200,
    msg: "添加成功",
  });
});

/*
 * 切换商品选中状态
 *   skuId   - 商品skuId.
 *   isChecked   - 商品选中状态，1为选中，0为未选中.
 */
router.get("/checkCart/:skuId/:isChecked", async (req, res) => {
  const { skuId, isChecked } = req.params;
  const data = await cartInfoListModel.updateOne({ skuId }, { isChecked });
  res.send({
    code: 200,
    msg: "skuId",
    data,
  });
});
// * 切换商品选中状态
//  *   skuId   - 商品skuId.
//  *   isChecked   - 商品选中状态，1为选中，0为未选中.
//  */
router.get("/shopcheckCart/:skuId/:isChecked", async (req, res) => {
  const { skuId, isChecked } = req.params;

  await skuProductListModel.updateMany({ _id: skuId }, { isChecked });

  res.send({
    code: 200,
    msg: "skuId",
  });
});

/*
 * 切换指定商品选中状态
 *   isChecked   - 商品选中状态，1为选中，0为未选中.
 *   skuIdList   - 商品skuId列表.
 */
router.post("/batchCheckCart/:isChecked", async (req, res) => {
  const data = await cartInfoListModel.find({ ...req.body });
  res.send({
    code: 200,
    msg: "切换商品",
    data,
  });
});

/*
 * 切换所有商品选中状态
 *   isChecked   - 商品选中状态，1为选中，0为未选中.
 */
router.get("/checkAllCart/:isChecked", async (req, res) => {
  if (req.params.isChecked == "true") {
    await cartInfoListModel.updateMany({}, { isChecked: 1 });
    await skuProductListModel.updateMany({}, { isChecked: 1 });
    const data = await cartInfoListModel.find();
    res.send({
      code: 200,
      msg: "isChecked",
      data,
    });
  } else {
    const data = await cartInfoListModel.updateMany({}, { isChecked: 0 });
    await skuProductListModel.updateMany({}, { isChecked: 0 });
    res.send({
      code: 200,
      msg: "isChecked",
      data,
    });
  }
});
router.get("/checkAllCart1/:isChecked", async (req, res) => {
  if (req.params.isChecked == "true") {
    await cartInfoListModel.updateMany({}, { isChecked: 1 });
    const data = await cartInfoListModel.find();
    res.send({
      code: 200,
      msg: "isChecked",
      data,
    });
  } else {
    const data = await cartInfoListModel.updateMany({}, { isChecked: 0 });

    res.send({
      code: 200,
      msg: "isChecked",
      data,
    });
  }
});
/*
 * 删除购物 车商品
 *   skuId   - 商品skuId.
 */
router.delete("/deleteCart/:skuId", async (req, res) => {
  await cartInfoListModel.deleteOne({ skuId: req.params.skuId });
  res.send({
    code: 200,
    msg: "skuId",
  });
});

// 获取带活动购物车对象
router.get("/activityCartList", async (req, res) => {
  const result = (await activityCartListModel.find())[0];
  const couponInfoList = await couponInfoListModel.find();
  const activityRule = await activityRuleModel.find();
  const cartInfoList = await cartInfoListModel.find();
  const carInfoVoList = [
    {
      activityRule,
      cartInfoList,
    },
  ];
  res.send({
    code: 200,
    msg: "获取成功",
    data: {
      couponInfoList,
      carInfoVoList,
      activityReduceAmount: result.activityReduceAmount,
      couponReduceAmount: result.couponReduceAmount,
      originalTotalAmount: result.originalTotalAmount,
      totalAmount: result.totalAmount,
    },
  });
});

module.exports = router;
